package dev.guide.userguide;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import dev.guide.R;
import dev.guide.userguide.ScrollLayout.PanelChangeListener;

public class UserGuideActivity extends Activity implements OnClickListener,
		PanelChangeListener {
	private ScrollLayout _scrollGuide;
	private int _indexCur = 0;
	private int _totalGuides = 0;
	private TextView _tvGuideTitle;
	private Button _btnPrev = null;
	private Button _btnNext = null;

	private static final int _arrGuideImg[] = { R.drawable.icon,
			R.drawable.advanced, R.drawable.app_web_browser_sm, };
	private static final int _arrDescId[] = { R.string.app_name,
			R.string.exit_app, R.string.next_page, };
	private ArrayList<String> _arrGuideDesc = new ArrayList<String>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_guide);

		Button btn = (Button) findViewById(R.id.btnExit);
		btn.setOnClickListener(this);

		btn = (Button) findViewById(R.id.btnPrev);
		btn.setOnClickListener(this);
		_btnPrev = btn;

		btn = (Button) findViewById(R.id.btnNext);
		btn.setOnClickListener(this);
		_btnNext = btn;

		_tvGuideTitle = (TextView) findViewById(R.id.tvGuid);

		_scrollGuide = (ScrollLayout) findViewById(R.id.scrollGuide);
		_scrollGuide.setPanelChangeListener(this);

		ImageView imgGuide = null;
		LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);

		for (int i = 0; i < _arrGuideImg.length; i++) {
			imgGuide = new ImageView(this);
			imgGuide.setScaleType(ScaleType.FIT_XY);
			imgGuide.setImageResource(_arrGuideImg[i]);
			_scrollGuide.addView(imgGuide, params);

			_arrGuideDesc.add(getResources().getString(_arrDescId[i]));
		}
		_totalGuides = _scrollGuide.getChildCount();
		_tvGuideTitle.setText(_arrGuideDesc.get(0));
		_btnPrev.setVisibility(View.INVISIBLE);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnExit:
			finish();
			break;
		case R.id.btnPrev:
			if (_indexCur - 1 > 0) {
				_indexCur = _indexCur - 1;
			} else if (_indexCur - 1 == 0) {
				_indexCur = 0;
				_btnPrev.setVisibility(View.INVISIBLE);
			} else {
				// never happen
			}
			_btnNext.setText(getResources().getString(R.string.next_page));
			_scrollGuide.snapToScreen(_indexCur);
			_tvGuideTitle.setText(_arrGuideDesc.get(_indexCur));

			break;
		case R.id.btnNext:
			if (_indexCur + 1 >= _totalGuides) {
				finish();
				return;

			} else if (_indexCur + 1 == _totalGuides - 1) {
				_btnNext.setText(getResources().getString(R.string.finish));
				_indexCur = _indexCur + 1;
			} else {
				_indexCur = _indexCur + 1;
			}
			_btnPrev.setVisibility(View.VISIBLE);
			_scrollGuide.snapToScreen(_indexCur);
			_tvGuideTitle.setText(_arrGuideDesc.get(_indexCur));

			break;
		default:

			break;
		}
	}

	@Override
	public void onPanelChange(int index) {
		// TODO Auto-generated method stub
		if (index >= _totalGuides) {
			return;
		}
		if (index == _indexCur) {
			return;
		}
		_indexCur = index;
		_tvGuideTitle.setText(_arrGuideDesc.get(_indexCur));

		if (_indexCur == 0) {
			_btnPrev.setVisibility(View.INVISIBLE);
		} else {
			_btnPrev.setVisibility(View.VISIBLE);
		}

		if (_indexCur == _totalGuides - 1) {
			_btnNext.setText(getResources().getString(R.string.finish));
		} else {
			_btnNext.setText(getResources().getString(R.string.next_page));
		}

	}

}